<!-- 我的页 -->
<template>
  <div class="user">
    <div class="user_top">
      <img :src="imgsrc" width="70" alt="" />
      <h3 v-if="islogined">{{ username }}</h3>
      <h3 v-else @click="login">点击登录</h3>
      <van-icon :name="islogined ? 'cross' : 'arrow'" @click="loginout" />
    </div>
    <!-- 模态框 -->
    <div class="modal" v-show="isShowModel">
      <div class="modal_bg" @click="isShowModel = false"></div>
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="danger" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
//模块化导入图片
// import img from "../assets/images/touxiang.png";
let img = require("../assets/images/touxiang.png");
// 引入登录接口
import { loginFn } from "@/utils/https";
//  引入Cookies
import Cookies from "js-cookie";
console.log(Cookies);
export default {
  data() {
    return {
      islogined: false, // 是否登录状态 默认false 不登陆
      imgsrc: img, // 图片模块
      isShowModel: false, // 是否显示模态框 默认不显示 false
      username: "", // 用户名
      password: "", // 密码
    };
  },
  created() {
    console.log(this);
    // 做一个登录判断
    let user = JSON.parse(localStorage.getItem("user"));
    if (user) {
      this.username = user.username;
      this.imgsrc = user.avatar;
      this.islogined = true;
    }
  },
  methods: {
    login() {
      //点击登录
      //显示模态框
      this.isShowModel = true;
    },
    onSubmit() {
      // 点击提交
      loginFn({ username: this.username, pwd: this.password }).then((res) => {
        console.log(res);
        if (res.data.errno == 0) {
          //提示
          this.$toast.success("登录成功");
          // 登陆注册成功
          this.islogined = true;
          this.isShowModel = false;
          this.username = res.data.data.userInfo.username;
          this.imgsrc = res.data.data.userInfo.avatar;
          // 将用户名和token 存到本地
          localStorage.setItem("user", JSON.stringify(res.data.data.userInfo));
          // localStorage.setItem("token");
          Cookies.set("token", res.data.data.token);
        }
      });
    },
    loginout() {
      // 退出登录
      if (this.islogined) {
        //退出逻辑
        this.$dialog
          .confirm({
            title: "提示",
            message: "确认要退出吗？",
          })
          .then(() => {
            // on confirm
            console.log("确认");
            //清掉token
            Cookies.remove("token");
            //清掉localstorage
            localStorage.removeItem("user");
            // 重新刷新页面
            this.$router.go(0);
          })
          .catch(() => {
            // on cancel
            console.log("取消");
          });
      }
    },
  },
};
</script>
<style lang='less' scoped>
/* @import url(); 引入css类 */
.user {
  .user_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: 10px;
    }
    h3 {
      flex: 1;
    }
  }
  .modal {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
}
</style>